<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

    <?php
    
        session_start();
        
        if(!isset($_SESSION['user'])) {
            header("Location: ../admViews/Login.php");
        } 
        
        require_once '../table_object/User.php';
        $User = new User();
        $UserVO = new UserVO;
        
        if(isset($_GET['action']) && mysql_real_escape_string($_GET['action']) === 'prepareUpdate') {
            
            if(isset($_GET['id'])) {
                $UserVO = $User->select(mysql_real_escape_string($_GET['id']));
            }
            
        } elseif(isset($_POST['action']) && get_post('action') === 'update') {
        
            if(isset($_POST['id']) &&
                isset($_POST['name']) &&
                isset($_POST['user']) &&
                isset($_POST['pwd'])) {
                
                $UserVO->set_id(get_post('id'));
                $UserVO->set_name(get_post('name'));
                $UserVO->set_user(get_post('user'));
                $UserVO->set_pwd(get_post('pwd'));
                $result = $User->update($UserVO);     
                
                if($result) {
                    header("Location: ./UserListAll.php");
                }
                
            }
            
        }
               
        function get_post($var) {
            return mysql_real_escape_string($_POST[$var]);
        }
                
    ?>

    <head>
        <script type="text/javascript" src="../js/User.js"></script>
	<link href="../css/Main.css" rel="stylesheet" type="text/css">
        <title>Web App Dev - Mcgill  - <?php echo $_SESSION['user']; ?></title>
    </head>
    
    <body>
        <h3>User Master - Update</h3>
        <form name="userForm" action="./UserUpdate.php" method="post" onsubmit="return validateForm()">
            <input type="hidden" name="id" value="<?php echo $UserVO->get_id(); ?>">
            <input type="hidden" name="action" value="update">
            <table class="tableinputdata">
                <tr>
                    <td align="right">
                        Name
                    </td>
                    <td>
                        <input type="text" name="name" value="<?php echo $UserVO->get_name(); ?>" autofocus/>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        User
                    </td>
                    <td>
                        <input type="text" name="user" value="<?php echo $UserVO->get_user(); ?>"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        Password
                    </td>
                    <td>
                        <input type="password" name="pwd" value=""/>
                    </td>
                </tr>
            </table>
            <table>
                <tr>
                    <td>
                        <input type="submit" value="Save">
                    </td>
                    <td>
                        <input type="button" value="List All" onclick="goTo('./UserListAll.php')"/>
                    </td>
                </tr>
            </table>
      </form>
      <p id="dbFailedMessage"><?php echo mysql_error(); ?>.</p>   
   </body>
</html>









